/**
 * 
 */
package com.zxc.caffeine;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;

/**
 * @author admin
 *
 */
public class CaffeineDemo {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		method1();

	}

	public static void method1(){
		Cache<String, String> cache = Caffeine.newBuilder().expireAfterWrite(10, TimeUnit.SECONDS).build();
		String value = cache.get("aaa", (t) -> {
			return "bbb"+System.currentTimeMillis();
		});
		System.out.println(value);
		value = cache.getIfPresent("aaa");
		System.out.println(value);
		Map<String,String> values = cache.getAll(() -> {
			List<String> l = new ArrayList<String>();
			l.add("ccc1");
			l.add("ccc2");
			l.add("ccc3");
			return l.iterator();
		}, (t) -> {
			Map<String, String> m = new HashMap<>();
			t.forEach((key)->{
				m.put(key, ""+System.currentTimeMillis());
			});
			return m;
		});
		
		for (Entry<String, String> entry : values.entrySet()) {
			System.out.println("key:"+entry.getKey()+";value:"+entry.getValue());
		}
	}
}
